/* api */
import { getBaoGPTSetting, updateBaoGPTSetting } from "@src/api/AIApi"
/* hooks */
import { useLoading } from "@hooks/useLoading"
/* model */
import MsgModel from "@model/MsgModel"
import { UpdateBaoGPTSettingModel } from "@model/param/in/AI"
import { SettingGPTModuleEnum } from "@src/modules/setting/gpt/model"
/* vue */
import { computed, reactive, Ref, ref } from "vue"
/* utils */
import LogUtil from '@src/util/log.ts'
import { message } from '@src/util/message';
/* types */
import { SettingGPTConfigType } from "@src/modules/setting/gpt/types"
import { isEmpty } from "@src/util/type"

function useBaoGPTSetting() {
  
  const {
    fetchBaoGPTSetting,
    setting,
    loading: fetchBaoGPTSettingLoading,
  } = useFetchBaoGPTSetting()
  
  const {
    fetchUpdateBaoGPTSetting,
    loading: fetchUpdateBaoGPTSettingLoading,
    isSuccess: fetchUpdateBaoGPTSettingSuccess,
  } = useFetchUpdateBaoGPTSetting()
  
  const loading = computed(() => {
    return (
      fetchBaoGPTSettingLoading.value
      || fetchUpdateBaoGPTSettingLoading.value
    )
  })
  
  const initialize = () => {
    return fetchBaoGPTSetting()
  }
  
  const isOpen = computed(() => setting.value.isOpenGptGray)
  
  const onChangeModuleListHandler = (value: string[]) => {
    fetchUpdateBaoGPTSetting({
      moduleList: value
    }).then(() => {
      if (fetchUpdateBaoGPTSettingSuccess.value) {
        fetchBaoGPTSetting()
      }
    })
  }
  
  const onChangeCommonModelHandler = (value: boolean) => {
    fetchUpdateBaoGPTSetting({
      useCommonModel: value
    }).then(() => {
      if (fetchUpdateBaoGPTSettingSuccess.value) {
        fetchBaoGPTSetting()
      }
    })
  }
  
  return {
    initialize,
    onChangeCommonModelHandler,
    onChangeModuleListHandler,
    setting,
    isOpen,
    loading
  }
  
}

/** 
 * @description 售后宝 小宝 GPT
*/
function useFetchBaoGPTSetting() {
  
  // 加载状态
  const { loading, setLoading, clearLoading } = useLoading()
  
  const defaultSetting: SettingGPTConfigType = {
    isOpenGptGray: false,
    useCommonModel: false,
    stream: false,
    moduleList: [],
  }
  
  // 配置
  const setting: Ref<SettingGPTConfigType> = ref(defaultSetting)
  
  // 获取小宝 GPT 设置
  const fetchBaoGPTSetting = () => {
    
    setLoading()
    
    return (
      getBaoGPTSetting().then(result => {
        
        if (MsgModel.isSuccess(result)) {
          const data = result.data || defaultSetting
          setting.value = data
        } else {
          message.error(result.message)
        }
        
        // 默认添加 Wiki 模块
        if (isEmpty(setting.value.moduleList)) {[
          setting.value.moduleList = [
            SettingGPTModuleEnum.Wiki
          ]
        ]}
        
      })
      .catch(error => {
        LogUtil.error(error, fetchBaoGPTSetting.name)
      })
      .finally(() => {
        clearLoading()
      })
    )
    
  }
  
  return {
    fetchBaoGPTSetting,
    setting,
    loading
  }
}

/** 
 * @description 更新 售后宝 小宝 GPT
*/
function useFetchUpdateBaoGPTSetting() {
  
  // 加载状态
  const { loading, setLoading, clearLoading } = useLoading()
  
  const isSuccess = ref(false)
  
  // 更新 小宝 GPT 设置
  const fetchUpdateBaoGPTSetting = (params: UpdateBaoGPTSettingModel) => {
    
    setLoading()
    
    return (
      updateBaoGPTSetting(params).then(result => {
        
        if (MsgModel.isSuccess(result)) {
          isSuccess.value = true
        } else {
          message.error(result.message)
        }
        
      })
      .catch(error => {
        LogUtil.error(error, fetchUpdateBaoGPTSetting.name)
      })
      .finally(() => {
        clearLoading()
      })
    )
    
  }
  
  return {
    fetchUpdateBaoGPTSetting,
    isSuccess,
    loading,
  }
}

export {
  useBaoGPTSetting,
  useFetchBaoGPTSetting,
  useFetchUpdateBaoGPTSetting
}
